package com.wyk.project.excel;


import com.alibaba.excel.EasyExcel;
import com.wyk.project.common.convention.exception.ServiceException;
import groovy.util.logging.Slf4j;
import jakarta.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author wangyongkang
 * @date 2025.3.9
 */

@Slf4j
public class ExportExcelUtil {

    private static final String EXCEL_PATHS = "C:\\Users\\11364\\Desktop\\excel";

    /**
     * 定时导出 excel
     * @param data 监控信息
     * @param name 短连接拼接分组名 如 name: 分组1(Xnfiwn)-导航到B站ehuw
     */
    public static void exportExcel(List<StatisticInfoModelExcel> data, String name)  {
//        response.setCharacterEncoding("utf-8");
//        response.setHeader("Content-disposition", "attachment; filename=" + name + "-" + UUID.randomUUID() + ".xlsx");
//        try {
//            EasyExcel.write(response.getOutputStream(), StatisticInfoModelExcel.class)
//                    .autoCloseStream(Boolean.FALSE).sheet("模板")
//                    .doWrite(data);
//        } catch (IOException e) {
//            log.error("导出报表失败{}", e.getMessage());
//            throw new ServiceException(e + name + "导出报表失败");
//        }

        String filePath = EXCEL_PATHS + File.separator +  name.split("-")[0];
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        filePath = filePath + File.separator + name + "-" + UUID.randomUUID() + ".xlsx";
        EasyExcel.write(filePath, StatisticInfoModelExcel.class).sheet(name.split("-")[1] + "统计信息").doWrite(data);

    }

}